package cian_f_rice.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import cian_f_rice.controller.ChoiceController;
import cian_f_rice.controller.DrawEdgeController;
import cian_f_rice.model.DecisionLinesEvent;

/**
 * Default application layout for A1.impl;
 * 
 * Feel free to use but leave attribution in place.
 * 
 * @author heineman
 */
public class DecisionLinesGUI extends JFrame {

	/** Maintain reference to model. */
	DecisionLinesEvent model;
	
	/** Relevant Boundary objects. */
	JPanel contentPane;
	JPanel choicesPanel;
	DecisionLinesPanel drawingPanel;
	JLabel status;
	
	/** Choices here. */
	JTextField[] choices;
	
	/** Boolean to begin adding edges */
	boolean addEdges = false;
	
	/**
	 * Sets up the application window and drawing canvas. Also installs controllers.
	 */
	public DecisionLinesGUI(DecisionLinesEvent m) {
		this.model = m;
		
		setTitle("Decision Lines");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		choicesPanel = new JPanel();
		choicesPanel.setLayout(null);
		choicesPanel.setBounds(10, 11, 464, 54);
		contentPane.add(choicesPanel);
		
		// add choice text fields in place.
		choices = new JTextField[model.numChoices];
		int width = 464/model.numChoices;
		for (int i = 0; i < model.numChoices; i++) {
			final int j = i;
			choices[i] = new JTextField(10);
			choices[i].setBounds(i*width, 10, width - 20, 24);
			choices[i].setAlignmentY(CENTER_ALIGNMENT);
			choicesPanel.add(choices[i]);
			
			// here is where you would consider registering a controller with each
			// choices[i] JTextField. When entering text into a choices[i] text field
			// and pressing return, your controller would be executed
			
			
			
			choices[i].addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					ChoiceController choiceController = new ChoiceController(model, DecisionLinesGUI.this, j);
					choiceController.process();
				}
			});
		}
		
		drawingPanel = new DecisionLinesPanel(model);
		drawingPanel.setBounds(10, 76, 464, 334);
		contentPane.add(drawingPanel);
		
		// here is where you would considering registering controllers to process
		// mouse events in the Drawing Panel.
		
		JButton beginButton = new JButton("Begin");
		beginButton.setBounds(10, 428, 89, 23);
		contentPane.add(beginButton);
		
		// here is where you would consider registering a controller to process 
		// action events when user clicks on the button.
		
		status = new JLabel("Try Me");
		status.setForeground(java.awt.Color.red);
		status.setBounds(109, 428, 360, 23);
		contentPane.add(status);
		
		// call status.setText(text) when you want to announce an error/warning; call this same method
		// with "" as parameter to clear the status information.
		
		
		//  Add an action listener for the "Begin" button
		/*
		 * Basically, it checks each choice and sees if it has an actual value for choiceName
		 * If this is true, then everyChoiceReady is incremented.
		 * If everyChoiceReady = model.numChoices, then we disable editing of the JTextFields,
		 * and set the addEdges flag to true - allowing for edges to be drawn.
		 */
		beginButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if (addEdges == false)
				{
					int everyChoiceReady = 0;
					
					for (int i = 0; i < model.numChoices; i++)
					{
						if (model.lines.get(i).getChoiceName() != null)
						{
							everyChoiceReady += 1;
						}
					}
					
					if (everyChoiceReady == model.numChoices)
					{
						
						for (int i = 0; i < model.numChoices; i++)					
						{
							choices[i].setEditable(false);
							addEdges = true;
						}
					}
					else
					{
						status.setText("Not all choices have been entered.");
					}
				}
				else
				{
					
					if (model.isDone())
					{
						model.prioritizedChoices();
						System.exit(EXIT_ON_CLOSE);
					}
					else
					{
						status.setText("Not all edges have been added.");
					}
				}
					
			}
		});
		
		DrawEdgeController edgeController = new DrawEdgeController(model, this);
		
		drawingPanel.addMouseListener(edgeController);
		
	}
	
		
	
	
	/** Make it possible for controllers to get access to status messsage. */
	public JLabel getStatus() {
		return status;
	}
	
	
	/**
	 *  Make it possible for controllers to change the status message.
	 * @param newStatus
	 */
	
	public void setStatus(String newStatus)
	{
		status.setText(newStatus);
	}
	
	/** 
	 * Return the 'i' choice so the controller can access it.
	 * @param i
	 * @return
	 */
	
	public JTextField getChoice(int i)
	{
		return choices[i];
	}
	
	/**
	 * Return the addEdges flag.
	 * @return
	 */
	
	public boolean getAddEdges()
	{
		return addEdges;
	}
	
	/**
	 * Set the addEdges flag - for JUnit testing.
	 */
	public void setAddEdges(boolean newFlag)
	{
		addEdges = newFlag;
	}
	
	/**
	 * Returns the decisionLinesCanvas
	 * @return
	 */
	
	public DecisionLinesPanel getDrawingPanel()
	{
		return drawingPanel;
	}
}
